//UI Helpers
//----------
require('js/lib/require_patch').monkeypatch(this); // Needed for Android

//simple helper to branch values based on OS
var osname = Ti.Platform.osname;
function os(androidValue,iosValue) {
	return (osname === 'android') ? androidValue : iosValue;
}

exports.os = os;

//Shared values for common use in view creation
var fontFamily = os('Droid Mono','Helvetica Neue');

exports.fontFamily = fontFamily;

//keystroke savings for view creation
function view(args) {
	return Ti.UI.createView(args);
}

exports.view = view;

//Return a Titanium label either with the passed text, or with the given i18n key
function label(text,args) {
	return Ti.UI.createLabel(globals.util.objectMerge({
		text:L(text,text),
		textAlign:'left',
		color:'#000',
		height:'auto',
		font: {
			fontFamily:fontFamily
		}
	},args|| {}));
};

exports.label = label;

//return a window with a custom header
function createWin(title,args) {
	if (!args) {
		args = {};
	}
	var win = Ti.UI.createWindow(globals.util.objectMerge({
		backgroundImage:'images/grain.png',
//		navBarHidden:true, //hides the navigation bar on the window - also has the effect of associating a window with an activity
		orientationModes: [
			Ti.UI.LANDSCAPE_LEFT,
			Ti.UI.LANDSCAPE_RIGHT,
			Ti.UI.PORTRAIT,
			Ti.UI.UPSIDE_PORTRAIT
		]
	},args|| {}));

	var header = headerView({title: title, height:headerHeight(args.headerHeight)});
	win.add(header);

	//create an API on this window to add views underneath our custom header
	var container = view({
		top:headerHeight(args.headerHeight),
		left:0,
		right:0,
		bottom:0
	});
	win.add(container);
	win.addChild = function(child) {
		container.add(child);
	};
	//create an API to add items to the window header
	win.addHeaderChild = function(child) {
		header.add(child);
	};
	//create an API to set the title of our custom window
	win.updateTitle = function(title) {
		var titleStr = L(title,title);
		header.updateLabel(titleStr);
	};
	//create an API to set the patient of our custom window
	win.updatePatient = function(patient) {
		var patientStr = L(patient,patient);
		header.updatePatient(patientStr);
	};
	//create an API to set the provider of our custom window
	win.updateProvider = function(provider) {
		var providerStr = L(provider,provider);
		header.updateProvider(providerStr);
	};
	return win;
};

exports.window = createWin;

//create an image view - save keystrokes
function img(args) {
	return Ti.UI.createImageView(args);
}

exports.image = img;

function button(args,onclick) {
	var btn = Ti.UI.createButton(args);
	btn.addEventListener('click', onclick);
	return btn;
}

exports.button = button;

//create a back button which exits the given window - use our custom window object
exports.addBackButtonView = function(win, title) {
	var btnLabel = title||'Back';
	var backBtn = view({
		width:70 * globals.deviceFactor,
		height:30 * globals.deviceFactor,
		backgroundImage:'images/back.png',
		left:5
	});
	backBtn.add(label(btnLabel,{
		textAlign:'center',
		color:'#fff',
		font:{
			fontWeight:'bold'
		}
	}));
	win.addHeaderChild(backBtn);
	
	backBtn.addEventListener('click', function() {
		win.close();
	});
};

exports.addCustomButtonView = function(win, button) {
	win.addHeaderChild(button);
}

exports.loadingWindow = function() {
	var window = Ti.UI.createWindow({
		height:Ti.Platform.displayCaps.platformHeight/2,
		width:Ti.Platform.displayCaps.platformWidth/2,
		backgroundColor:'#787878',
		borderRadius:10,
		layout:'vertical',
		zIndex:99999
	});
	
	window.add(img({
		image:'images/target.png',
		height:50,
		width:50,
		top:10
	}));
	
	window.add(label('Loading...',{
		color:'#fff'
	}));
	
	return window;
};

function headerView(/*Object*/ _args) {
	globals.headerHeight = headerHeight(_args.height);
	var args = globals.util.objectMerge({
		backgroundColor:'#77B121',
        height:globals.headerHeight,
        top: 0
	}, _args);

    var v = view(args);
    var la = {
    	color: '#fff',
    	textAlign: 'center',
    	top: 0, height: 50 * globals.deviceFactor,
    	font:{fontSize:24 * globals.deviceFactor, fontWeight:'bold', fontFamily:fontFamily},
    	width: '95%'
    };
    // Title
    var labelStr = label(_args.title||'Patient Management', la);
    v.add(labelStr);
    
    v.updateLabel = function(title) {
    	labelStr.text = title;
    };

    la.top = 55 * globals.deviceFactor, la.textAlign = 'left', la.height = 20 * globals.deviceFactor, la.font = {fontSize:12 * globals.deviceFactor, fontStyle:'normal'};

    // Provider
    la.textAlign = 'right', la.width = '50%', la.left = null, la.right = 5;
	var providerLbl = label('', la);
	v.add(providerLbl);
    if (globals.provider) {
    	providerLbl.text = 'Provider: ' + globals.provider.firstName;
    }
    v.updateProvider = function(provider) {
    	providerLbl.text = 'Provider: ' + provider;
    };
    // Patient
	la.textAlign = 'left', la.width = '50%', la.left = 5, la.right = null;
    var patientLbl = label('', la);
	patientLbl.addEventListener('click',function(e){
		globals.patientWin.open();
	});
    v.add(patientLbl);
    if (globals.patient) {
    	patientLbl.text = 'Patient: ' + globals.patient.fullName();
    }
    v.updatePatient = function(patient) {
    	patientLbl.text = 'Patient: ' + patient;
    };
    
    return v;
};

exports.createHeaderView = headerView;

function headerHeight(height) {
	var heightVal = height||80;
	return heightVal *= globals.deviceFactor;
}

// Build a control bar with at least one button
exports.controlBar = function(titles, listeners, params) {
	Ti.API.debug('Top calc is: ' + ((Ti.Platform.displayCaps.platformHeight-(50 * globals.deviceFactor)) - globals.headerHeight - 265));
	var controlView = view(globals.util.objectMerge({
//		top: (Ti.Platform.displayCaps.platformHeight-50 * globals.deviceFactor) - globals.headerHeight - 265,
		bottom: 0,
		height: 50 * globals.deviceFactor, width: '100%',
		backgroundImage: globals.util.urls()['bg_toolbar']
	},params));
	
	var backBtn = button(
		{
			backgroundImage: globals.util.urls()['bg_back'],
			title: titles.back,
			width: '30%', height: '50%',
//			backgroundLeftCap: 12,
			left: 15
		},
		listeners.back
	);
	controlView.add(backBtn);
	
	var nextBtn = button({
			backgroundImage: globals.util.urls()['bg_next'],
			title: titles.next||'',
			width: '30%', height: '50%',
//			backgroundLeftCap: 3,
			right: 15
		},
		listeners.next||function(e){}
	);
	if (titles.next)
		controlView.add(nextBtn);
	
	var homeBtn = button({
			backgroundImage: globals.util.urls()['bg_home'],
			title: titles.home||'',
			width: '30%', height: '50%'
		},
		listeners.home||function(e){}
	);
	if (titles.home)
		controlView.add(homeBtn);
	
	// Add APIs
	controlView.addNextControl = function() {
		controlView.add(nextBtn);
	}
	controlView.removeNextControl = function() {
		controlView.remove(nextBtn);
	}
	
	return controlView;
}